package com.frt.sygang.controller;/**
 * Created by 冯瑞涛 on 2022/9/17 10:16
 */

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.frt.sygang.common.R;
import com.frt.sygang.entity.Employee;
import com.frt.sygang.sevice.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;

/**
 * @author 冯瑞涛
 * @version 1.0
 * @date 2022/9/17 10:16
 */
@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    /**
     * 登录方法
     *
     * @param request  用于登录成功后创建session
     * @param employee 前端传输的数据，是使用对象接收
     * @return
     */
    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee) {
  /*
  5.返回登录成功信息
   */
        // 1.将接收到的密码进行mp5加密
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        //2.使用用户名查询数据库
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername, employee.getUsername());
        Employee empLon = employeeService.getOne(queryWrapper);
        if (empLon == null) {
            return R.error("登录失败，用户名错误");
        }
        //3.验证密码是否正确
        if (!empLon.getPassword().equals(password)) {
            return R.error("登录失败，密码错误");
        }
        //  4.验证用户是否被限制
        if (empLon.getStatus() != 1) {
            return R.error("用户被限制登录，请联系管理员");
        }
        request.getSession().setAttribute("employee", empLon.getId());
        return R.success(empLon);
    }

    /**
     * 退出登录
     *
     * @param request 获取session并清除
     * @return
     */
    @PostMapping("/logout")
    public R<String> loginOut(HttpServletRequest request) {
        //删除储存的session中的数据
        request.getSession().removeAttribute("employee");
        //返回结果
        return R.success("退出成功");
    }

    /**
     * 增加员工方法
     *
     * @param employee 前端传输的数据用对象接收
     * @return
     */
    @PostMapping
    public R<String> insUser(@RequestBody Employee employee) {
        //给一个默认密码使用MD5加密
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        //进行添加
        boolean save = employeeService.save(employee);
        if (save) {
            return R.success(null);
        }
        return R.error(null);
    }

    /**
     * 分页查询
     * @param page 第几页
     * @param pageSize 一页几条
     * @param name 要查询的姓名
     * @return
     */
    @GetMapping("/page")
    public R<IPage> pageQuery(Integer page, Integer pageSize, String name) {
        //1.新建一个分页构造器
        IPage<Employee> iPage = new Page<>(page, pageSize);
        //2.新建一个条件构造器
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(!StringUtils.isEmpty(name), Employee::getName, name);
        queryWrapper.orderByDesc(Employee::getUpdateTime);
        employeeService.page(iPage, queryWrapper);
        return R.success(iPage);
    }

    /**
     * 用户账号状态更新
     * @param employee 实体类对象
     * @return
     */
    @PutMapping
    public R<String> updateEmp(@RequestBody Employee employee){
        boolean b = employeeService.updateById(employee);
        if(b){
            return R.success("用户信息修改成功！");
        }
        return R.success("修改失败！");
    }

    @GetMapping("/{id}")
    public R<Employee> getEmp(@PathVariable Long id){
        Employee emp = employeeService.getById(id);
        if(emp!=null){
            return R.success(emp);
        }
        return R.error("未查询到对应员工信息");
    }
}
